package com.webSite.demo.cy.downBook;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;

public class DownImage {

	public static void main(String[] args) {

		String content = HttpUtil.get("http://www.kuuhuu.com/shu/94/",CharsetUtil.CHARSET_GBK);
		Document doc = Jsoup.parse(content);
		Elements links = doc.body().select(".list li a");
		int i=0;
		for (Element link : links) {
			if(i>284) {
				String linkHref = link.attr("href");
				String linkText = link.text();
				downImg(linkHref,linkText);
			}
			i++;
		}
		
	}

	

	private static void downImg(String linkHref, String linkText) {
		String content = HttpUtil.get("http://www.kuuhuu.com"+linkHref,CharsetUtil.CHARSET_GBK);
		
		Document doc = Jsoup.parse(content);
		Element img = doc.body().select("#booktext .divimage img").get(0);
		
		long size = HttpUtil.downloadFile(getImgName(img.attr("src")), FileUtil.file("C:\\Users\\fengzihao\\Desktop\\12121\\"+linkText+".gif"));
		System.out.println("Download size: " + size+" 图片路径 : "+getImgName(img.attr("src")));
	}



	/**
	 * 获取图片的名字
	* @Title: getImgName  
	* @Description: TODO()  
	* @param @param fileUrl
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	private static String getImgName(String fileUrl) {
		StringBuilder builder = new StringBuilder("http://www.kuuhuu.com.img.800cdn.com/files/article/");
		builder.append(fileUrl.substring(fileUrl.indexOf("attachment"),fileUrl.length()));
		return builder.toString();
	}
	
}
